package com.uxwine.alarm.main;

import java.util.Calendar;

import com.uxwine.alarm.AlarmActivity;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.text.format.DateFormat;
import android.widget.Toast;

public class Alarmer {
	
	private AlarmManager mAm;
	private Context mCtx;
	
	public Alarmer(Context ctx)
	{
		mCtx = ctx;
		mAm = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
	}
	
	public boolean start(Alarm a)
	{
		if(a == null)
			return false;
		long time = a.getTriggerTime();
		if(time <= Calendar.getInstance().getTimeInMillis() + 1000)
		{
			return false;
		}
		String msg = "Trigge Time:" + DateFormat.format("yyyy-MM-dd kk:mm", time);		
		Toast.makeText(mCtx, msg, Toast.LENGTH_LONG).show();
		
		mAm.set(AlarmManager.RTC_WAKEUP, time, getOperation(a));
		return true;
	}
	
	public void cancel(Alarm a)
	{
		mAm.cancel(getOperation(a));
	}
	
	private PendingIntent getOperation(Alarm a)
	{
		Intent intent = new Intent(mCtx, ClockActivity.class);
		intent.putExtra("id", a.getId());
		PendingIntent pi = PendingIntent.getActivity(mCtx, 0, intent, 0);
		return pi;
	}
	
	
}
